<template>
  <div
    style="background-color: #f0f2f5; min-height: 100vh; display: flex; justify-content: center; align-items: center;">
    <el-form :model="loginForm" :rules="rules" ref="loginFormRef" label-width="80px"
      style="background-color: #ffffff; padding: 20px;">
      <h1 color-blue>迈科文件服务</h1>
      <el-form-item label="用户名" prop="username">
        <el-input v-model.trim="loginForm.username" @keyup.enter="onSubmit" ref="usernameRef" />
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input type="password" v-model.trim="loginForm.password" @keyup.enter="onSubmit" ref="passwordRef" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit">登录</el-button>
        <el-button @click="onReset">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script setup ts>
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'
import { loginInfo } from '@/store/loginInfo'
const router = useRouter()

const rules = reactive({
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' }
  ]
})

const loginForm = reactive({
  username: undefined,
  password: undefined
})

const loginFormRef = ref()
const usernameRef = ref()
const passwordRef = ref(null)

const onSubmit = () => {
  if (!loginFormRef.value) return;
  loginFormRef.value.validate((valid) => {
    if (valid) {
      console.log('表单数据:', loginForm);
      // 这里可以添加登录请求逻辑
      loginInfo.setIsLogin(true);
      router.push('/');
    } else {
      if (!loginForm.username || loginForm.username == '') {
        console.log('请输入用户名');
        usernameRef.value.focus();
      } else if (!loginForm.password) {
        console.log('请输入密码');
        passwordRef.value.focus();
      }
      console.log('表单验证失败');
      return false;
    }
  });
};

const onReset = () => {
  loginForm = { username: '', password: '' }
}

</script>